//
//  ClosureStudy.swift
//  Swift3.0_Study
//
//  Created by chengfeng on 2016/9/27.
//  Copyright © 2016年 zhaochengfeng. All rights reserved.
//

import Cocoa

class ClosureStudy: NSObject {
//闭包是一种函数的简写形式, 省去函数名, 把参数和返回值放入花括号内.
    func start() -> Void {
        // { (a: Int, b: Int) -> Int in
        //      执行语句
        //      return ...
        // }
        
        let city = ["zhengzhou","xiamen","hefei","nanchang"]
        let cityRank1 = city.sorted()
        print(city)
        print(cityRank1)
        let cityRank3 = city.sorted { (a, b) -> Bool in
            return a > b
        }
        print(cityRank3)
        
        //③可使用快捷参数,前缀$,从0开始递增
        let cityRank5 = city.sorted { $0 > $1 }
        print(cityRank5)
        
        loadData { (res) in
            print(res)
        }
        
        loadRemoteData(para: false, success: { (res) in
            print(res)
            }){ (err) in
                print("error")
        }
  
        self.printData(arr: [4, 5, 6]) { (a) -> Bool in
            return a > 5
        }
        
        var arr = [1, 4, 5, 6, 2]
        arr.sort {(a, b) in
            return a > b
        }
        
        print(arr)
        
    }
    
    func loadData(success: (AnyObject) -> Void) -> Void {
        success("成功了" as AnyObject)
    }
    
    func loadRemoteData(para: Bool, success: (AnyObject) -> Void, failure:(Error?) -> Void) -> Void {
        if para {
            success("success" as AnyObject)
        }else {
            failure(nil)
        }
    }
    
    func printData(arr: [Int], condition: (Int) -> Bool) -> Void {
        for n in arr {
            if condition(n) {
                print(n)
            }else {
                print("\(n) 不符合")
            }
        }
    }
}
